import java.math.BigInteger;


public class RSA {
	
	
	
	public static String encodeRSA(String plain,int n, int e){
		
		BigInteger BigN= BigInteger.valueOf(n);
		BigInteger BigE= BigInteger.valueOf(e);
		String chiper = "";
		for (int i = 0; i < plain.length(); i++)
		{
			BigInteger BigM = new java.math.BigInteger(""+(int) plain.charAt(i));
			int c = BigM.modPow(BigE,BigN).intValue();
			chiper += (char)c;
			}
		return chiper;
	}
	
	
	public static String decodeRSA(String chiper,int n, int d)
	{
		BigInteger BigN= BigInteger.valueOf(n);
		BigInteger BigD= BigInteger.valueOf(d);
		String decoded = "";
		for (int i = 0; i < chiper.length(); i++)
		{
			BigInteger BigM = new java.math.BigInteger(""+(int) chiper.charAt(i));
			int c = BigM.modPow(BigD,BigN).intValue();
			decoded += (char)c;
			}
		return decoded;
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/*
		 * Zahlen
		 * n = 2773
		 * e = 17
		 * d = 157
		 */
		
		int n = 2773;
		String m ="Wie gehts den so??? Mir gehts gut und dir???";
		System.out.println("Message: "+m);
		int e = 17;
		int d = 157;
	

		
		
		String chiper = encodeRSA(m,n,e);
		System.out.println("Encoded: "+chiper);
		String message = decodeRSA(chiper,n,d);
		System.out.println("Decoded: "+message);
		
		
		
	}

}
